/* 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.alcina.framework.common.client.remote;

import cc.alcina.framework.gwt.client.logic.AlcinaDebugIds;
import cc.alcina.framework.gwt.client.rpc.AlcinaRpcRequestBuilder;

import com.google.gwt.user.client.rpc.RpcRequestBuilder;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

/**
 * 
 * @author Nick Reddel
 */
public abstract class CommonRemoteServiceAsyncProvider<CRSA extends CommonRemoteServiceAsync> {
	public CRSA getServiceInstance() {
		CRSA service = createAndIntialiseEndpoint();
		((ServiceDefTarget) service)
				.setRpcRequestBuilder(getRequestBuilder());
		return service;
	}
	public CRSA getServiceInstance(RpcRequestBuilder builder) {
		CRSA service = createAndIntialiseEndpoint();
		((ServiceDefTarget) service)
				.setRpcRequestBuilder(builder);
		return service;
	}
	public AlcinaRpcRequestBuilder getRequestBuilder(){
		return new AlcinaRpcRequestBuilder();
	}
	protected abstract CRSA createAndIntialiseEndpoint();
	
	protected String adjustEndpoint(String endpoint){
		if (AlcinaDebugIds.hasFlag(AlcinaDebugIds.DEBUG_SIMULATE_OFFLINE)){
			endpoint+="-not";
		}
		return endpoint;
	}
	
	
}
